﻿using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace App_Code
{
    /// <summary>
    /// Summary description for MapScriptBuilder
    /// </summary>
    public class MapScriptBuilder
    {
        static readonly Dictionary<string, PlaceCoordinate> _places = new Dictionary<string, PlaceCoordinate>();

        /// <summary>
        /// handles the complexity of building javascript and inserting it into the response stream
        /// </summary>
        static MapScriptBuilder()
        {
            _places.Add("laos", new PlaceCoordinate(17.8, 103.7, 7));
            _places.Add("northernlaos", new PlaceCoordinate(19.6, 102.4, 8));
            _places.Add("centrallaos", new PlaceCoordinate(18.3, 103, 8)); 
            _places.Add("southernlaos", new PlaceCoordinate(16.4, 105.6, 8));
            _places.Add("luangprabang", new PlaceCoordinate(19.88, 102.141, 15));
            _places.Add("vientianne", new PlaceCoordinate(17.96, 102.61, 13));
        }

        public static string BuildMapOptionStringByPlaceName(string queryString)
        {
            // HTTP puts '+' in for spaces

            string key = queryString.ToLower().Replace("+", "").Replace(" ", "");

            PlaceCoordinate placeCoordinate;
            if (_places.TryGetValue(key, out placeCoordinate))
            {
                return BuildMapOptionString(placeCoordinate);
            }

            return BuildMapOptionString(new PlaceCoordinate(17.8, 103.7, 7));
        }

        /*    
     
    "var latlng = new google.maps.LatLng(52.04, -0.75);"+
    "myOptions = {zoom: 12, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP };" 
     
   * */
        private static string BuildMapOptionString(PlaceCoordinate place)
        {
            var sb = new StringBuilder();
            sb.Append("var latlng = new google.maps.LatLng(");
            sb.Append(string.Format(CultureInfo.InvariantCulture, "{0:0.##}, {1:0.##}", place._latitude, place._longitude));
            sb.Append(");" + "myOptions = {zoom:");
            sb.Append(place._zoom);
            sb.Append(", center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP };");
            return sb.ToString();
        }
    }
}